﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
using Microsoft.Extensions.DependencyInjection;

namespace Salary.Sys.Cmdline
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var db = new Database();

            if (db.Database.GetPendingMigrations().Any())
                db.Database.Migrate();

            var v = Guid.NewGuid().ToString("N");
        }
    }

    public class Migration : Microsoft.EntityFrameworkCore.Migrations.Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable("table", c => new
            {
                Key = c.Column<string>(nullable: false),
                Value = c.Column<string>(nullable: false)
            }, null, t => t.PrimaryKey("PK_table", m => m.Key));
        }
    }

    public class Setting
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }


    public partial class Database : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlite("Filename=settings.db");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Setting>().HasKey(t => t.Key);

            modelBuilder.Entity<Setting>().Property(t => t.Key).IsRequired();
            modelBuilder.Entity<Setting>().Property(t => t.Value).IsRequired();
        }


        public virtual DbSet<Setting> Settings { get; set; }
    }
}
